package top.littlejiang.onlinexam.common.utils;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 时间 工具类
 * @author chen
 * @Description
 * @since 2020/12/26 16:31
 */
public class DateUtils {
    /**
     * 时间格式(yyyy-MM-dd)
     */
    public final static String DATE_PATTERN_10 = "yyyy-MM-dd";
    /**
     * 时间格式(yyyy-MM-dd HH:mm:ss)
     */
    public final static String DATE_TIME_PATTERN_19 = "yyyy-MM-dd HH:mm:ss";

    /**
     * 时间格式(yyyyMMddHHmmss)
     */
    public final static String DATE_TIME_PATTERN_14 = "yyyyMMddHHmmss";

    /**
     * 时间格式(yyyyMMdd)
     */
    public final static String DATE_TIME_PATTERN_8 = "yyyyMMdd";

    public static String format(Date date) {
        return format(date, DATE_PATTERN_10);
    }

    public static String format(Date date, String pattern) {
        if (date != null) {
            SimpleDateFormat df = new SimpleDateFormat(pattern);
            return df.format(date);
        }
        return null;
    }
    /**
    *获取当前时间，以便存入数据库
     */
    public static Date getTime(){
        Date date = new Date();
        String str = DateUtils.format(date, DATE_TIME_PATTERN_19);
        Timestamp timestamp =Timestamp.valueOf(str);
        return timestamp;
    }

    public static String toCornString(Date date){
        Calendar ca = Calendar.getInstance();
        ca.setTime(date);
        String month = String.valueOf(ca.get(Calendar.MONTH) + 1);
        String day = String.valueOf(ca.get(Calendar.DATE));
        String hour = String.valueOf(ca.get(Calendar.HOUR_OF_DAY));
        String minute = String.valueOf(ca.get(Calendar.MINUTE));
        String second = String.valueOf(ca.get(Calendar.SECOND));

        return second + " " + minute + " " + hour + " " +day + " " + month + " ? ";
    }



}
